<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
     <el-form-item label="借款额度">
        <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="integralGrade.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="integralGrade.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()">
          保存
        </el-button>
      </el-form-item>
    </el-form>    
      </div>
</template>
<script>
import integralGrade from "@/api/core/integral-grade"
export default {
    data() {
        return {
            saveBtnDisabled: false, // 是否禁用表单按钮，防止表单重复提交
            integralGrade: {} // 积分等级对象
        }
    },

    created() {
        // 当路由中存在id属性时，回显表单
        if (this.$route.params.id) {
            this.fetchById(this.$route.params.id);
        }
        
    },

    methods: {
        // 通过id得到列表数据
        fetchById(id) {
            integralGrade.getById(id)
            .then(response => {
                this.integralGrade = response.data.record;
            })
        },
        // 保存或更新积分等级
        saveOrUpdate() {
            if (this.$route.params.id != null) {
                this.saveBtnDisabled = true, // 禁用保存按钮
                this.updateData();
            } else {
                this.saveBtnDisabled = true, // 禁用保存按钮
                // 调用新增
                this.saveData();
            }
            
            // 调用更新
        },

        saveData() {
            integralGrade.save(this.integralGrade)
            .then(response => {
                this.$message({
                    type: "success",
                    message: response.message,
                })

                this.$router.push("/core/integral-grade/list");
            })
            .catch(error => {
                this.$message.error("保存积分列表失败");
            });
        },
        updateData() {
            integralGrade.updateList(this.integralGrade)
            .then(response => {
                this.$message({
                    type: "success",
                    message: "更新记录成功",
                })
                this.$router.push("/core/integral-grade/list");
            })
            .catch(error => {
                this.$message.error("更新积分列表失败");
            })
            
        }
    },
}
</script>

<style scoped>

</style>